package org.example.registry.redis;

import redis.clients.jedis.Jedis;

/**
 * @Author Jiangxiaonan
 * @Description
 * @Date 2024/9/15 10:42
 **/
public class RedisOperations {

    public void set(String key, String value) {
        try (Jedis jedis = RedisConnectionManager.getResource()) {
            jedis.set(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setKeyWithExpirationUsingSetEx(String key, String value, int seconds) {
        try (Jedis jedis = RedisConnectionManager.getResource()) {
            jedis.setex(key, seconds, value); // 设置键值对并设置过期时间为 seconds 秒
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String get(String key) {
        try (Jedis jedis = RedisConnectionManager.getResource()) {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void delete(String key) {
        try (Jedis jedis = RedisConnectionManager.getResource()) {
            jedis.del(key);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void destroy() {
        try (Jedis jedis = RedisConnectionManager.getResource()) {
            jedis.flushAll();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
